# setup
#编译选项
COMPILE_OPTS = -c -mcpu=cortex-m3  -mthumb  -mlittle-endian  -W -Wall -g -Werror  -nostartfiles
#包含的文件目录
INCLUDE_DIRS += -I../LibrariesST/inc  


OBJ_DIR= .


UserOBJ+= $(patsubst %.c,%.o,$(wildcard *.c))



CC = arm-none-eabi-gcc
CFLAGS = $(COMPILE_OPTS) $(INCLUDE_DIRS) -D STM32F10X_MD -D VECT_TAB_FLASH  -D USE_STDPERIPH_DRIVER -O1

CXX = arm-none-eabi-g++
CXXFLAGS = $(COMPILE_OPTS) $(INCLUDE_DIRS)

AS = arm-none-eabi-gcc
ASFLAGS = $(COMPILE_OPTS) -c

LD = arm-none-eabi-gcc
LDFLAGS =  -T ../LibrariesST/links/stm32_flash_bl.ld -Wl,-Map=$(OBJ_DIR)/output.map --specs=nosys.specs


SIZE =arm-none-eabi-size

OBJCP = arm-none-eabi-objcopy
OBJCPFLAGS_BIN = -O binary
OBJCPFLAGS_HEX = -O ihex
#link option
AR = arm-none-eabi-ar
ARFLAGS = cr


Target_Name = "bootloader"
ST_LIB = "../LibrariesST/libstm32.a"
 
all: $(Target_Name).hex $(Target_Name).bin 
	
	
$(Target_Name).elf: $(UserOBJ)
	@echo "Building $@ ..."
	@$(LD) $(LDFLAGS) $(UserOBJ) $(ST_LIB)  --output $(OBJ_DIR)/$@
	@$(SIZE) -d -t $(OBJ_DIR)/$@
	@echo "Building $@ Success!"

$(Target_Name).hex : $(Target_Name).elf 
	@echo "Building $@ ..."
	@$(OBJCP) $(OBJCPFLAGS_HEX) $(OBJ_DIR)/$(Target_Name).elf $(OBJ_DIR)/$@
	@echo "Building $@ Success!"


$(Target_Name).bin : $(Target_Name).elf
	@echo "Building $@ ..."
	@$(OBJCP) $(OBJCPFLAGS_BIN) $(OBJ_DIR)/$(Target_Name).elf $(OBJ_DIR)/$@
	@printf "\nBinFileSize: 0x%x Bytes\n\n" $$(stat -c "%s" $@)
	@echo "Building $@ Success!"
	
	

%.o:%.c
	@echo CC: $<
	@$(CC) $(CFLAGS) -c $< -o $@


.PHONY: clean

clean:
	@-rm -rf  $(Target_Name).elf $(Target_Name).bin $(Target_Name).hex  $(UserOBJ) *.map